﻿	<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'auction_userwonpaid';
</cfscript>

<!---已支付单--->
<cffunction access="remote" name="userpaidrecord" returntype="string">
<cfquery datasource="#DB#" name="userpaidrecord">
select count(a.uid) as rc from auction_userlastbid   a  left outer join auction b on a.aid=b.aid  left outer join auction_special c on b.asid=c.asid  left outer join auction_userwon d on a.wonID=d.wonID left outer join auction_userwonpaid e on d.paidID=e.paidID   where a.uid= #uid#
and a.is_bid=1 and a.pay_status=2  and a.aid in (select aid from auction where asid in (select asid from auction_special where now() > asclosetime) )
</cfquery>
<cfreturn userpaidrecord.rc>
</cffunction>

<!---已支付单--->
<cffunction access="remote" name="userunpaidrecord" returntype="string">
<cfquery datasource="#DB#" name="userunpaidrecord">
SELECT
count(1) as rc
FROM
auction_userwonpaid
WHERE
uid = #uid# AND pay_status < 2
AND auction_userwonpaid.acttime IS NOT NULL
</cfquery>
<cfreturn userunpaidrecord.rc>
</cffunction>

<!---支付单总数--->
<cffunction access="public" name="allpaidlistrecord" returntype="query">
<cfquery datasource="#DB#" name="allpaidlistrecord">
<!--- 原sql  按照方法明分析，应该列出该用户全部支付单信息，所以不应该加其它条件
SELECT
count(1) as rc
FROM
auction_userwonpaid
WHERE
uid = #uid#
AND auction_userwonpaid.acttime IS NOT NULL AND
task = 0 AND
ftask = 0 AND
payed = 0
--->
<!--- 显示未支付订单记录数 --->
SELECT count(1) as rc FROM auction_userwonpaid WHERE uid = #uid# and pay_status=0
</cfquery>
<cfreturn allpaidlistrecord>
</cffunction>

<!---未发货拍品总数--->
<cffunction access="public" name="unfilledrecord" returntype="string">
<cfquery datasource="#DB#" name="unfilledrecord">
SELECT
count(1) as rc
FROM
auction_userwonpaid
INNER JOIN auction_userwon ON auction_userwon.paidID = auction_userwonpaid.paidID
WHERE auction_userwonpaid.uid = #uid#
AND (ship_status = 0 or ship_status = 3)
AND auction_userwonpaid.acttime IS NOT NULL
</cfquery>
<cfreturn unfilledrecord.rc>
</cffunction>

<!---已发货拍品总数--->
<cffunction access="public" name="sendOutrecord" returntype="string">
<cfquery datasource="#DB#" name="sendOutrecord">

select count(a.uid) as rc from auction_userlastbid   a  left outer join auction b on a.aid=b.aid  left outer join auction_special c on b.asid=c.asid  left outer join auction_userwon d on a.wonID=d.wonID left outer join auction_userwonpaid e on d.paidID=e.paidID   where a.uid=#uid#

and a.is_bid=1 and a.pay_status=2    and a.aid in (select aid from auction where asid in (select asid from auction_special where now() > asclosetime) )

 and a.aulbid  in (select aulbid from  auction_userwon   where paidID in (select paidID from auction_userwonpaid where pay_status=2 and ship_status=2)  )

</cfquery>
<cfreturn sendOutrecord.rc>
</cffunction>


<!---生成拍卖单--->
<cffunction access="public" name="createwonpaid" returntype="string">
<cfparam name="fare" default="0">
<cfscript>
sumba = 0;
//GENERATEDKEY
</cfscript>
<!---显示会员地址--->
<cfquery datasource="#DB#" name="shipaddr">
select *
from user_addresses
where uaid=#uaid#
</cfquery>
<!---支付选择--->
<cfquery datasource="#DB#" name="payment">
select pay_id,pay_name
from set_payment
where pay_id = '#pay_id#'
</cfquery>


<cfscript>
pay_id = payment.pay_id;
pay_name = payment.pay_name;

ship_name = shipaddr.receiveName;
ship_area = shipaddr.province & shipaddr.city & shipaddr.district;
ship_addr = shipaddr.street;
ship_zip = shipaddr.zipcode;
ship_tel = shipaddr.phone;
ship_mobile = shipaddr.mobile;
</cfscript>

<!---创建空单--->
<!---<cfquery datasource="#DB#" result="rs">
insert into auction_userwonpaid(uid,fare,invoicetitle,remark,createtime,ship_name,ship_area,ship_addr,ship_zip,ship_tel,ship_mobile)
value(#session.user.uid#,#fare#,'#form.invoicetitle#','#form.remark#',#now()#,'#form.ship_name#','#form.ship_area#','#form.ship_addr#','#form.ship_zip#','#form.ship_tel#','#form.ship_mobile#')
</cfquery>--->
<cfquery datasource="#DB#" result="rs">
insert into auction_userwonpaid(uid,fare,pay_id,payment,createtime,ship_addrid,ship_name,ship_area,ship_addr,ship_zip,ship_tel,ship_mobile,yunfei,remark)
value(#uid#,#fare#,#pay_id#,'#pay_name#',#now()#,#uaid#,'#ship_name#','#ship_area#','#ship_addr#','#ship_zip#','#ship_tel#','#ship_mobile#',#fare#,'#remarks#')
</cfquery>

<cfquery datasource="#DB#" name="wonbid">
SELECT
auction_userlastbid.aulbid,
auction_userlastbid.aid,
auction.acode,
auction.auction,
auction.endtime,
auction_userlastbid.bid,
auction.servicefee
FROM
auction_userlastbid
INNER JOIN auction ON auction.aid = auction_userlastbid.aid
WHERE is_bid = 1 AND auction_userlastbid.uid = #uid#
AND auction.aid IN (#aidlist#)
</cfquery>

<cfif wonbid.recordcount gt 0>
	<cfoutput query="wonbid">
  	<cfscript>
		bid_fee = servicefee * 0.01 * bid;
		bid_amount = bid + bid_fee;
		sumba = sumba + bid_amount;
		limittime = dateadd('d',14,endtime);
		</cfscript>
    <cfquery datasource="#DB#" result="rsw">
    insert into auction_userwon(paidID,uid,aid,acode,auction,endtime,limittime,bid,bid_fee,bid_amount,aulbid)
    value(#rs.GENERATEDKEY#,#uid#,#aid#,'#acode#','#auction#','#endtime#',#limittime#,#bid#,#bid_fee#,#bid_amount#,#aulbid#)
    </cfquery>
    <cfquery datasource="#DB#">
    update auction_userlastbid set
    wonID = #rsw.GENERATEDKEY#
    where aulbid = #aulbid#
    </cfquery>
  </cfoutput>
  <!---激活支付单--->
  <cfquery datasource="#DB#">
  update auction_userwonpaid set
  bids_cost = #sumba#,
  total_amount = #sumba + fare#,
  final_amount = #sumba + fare#,
  acttime = #now()#
  where paidID = #rs.GENERATEDKEY#
  </cfquery>
  <!---支付单日志--->
  <cfquery datasource="#DB#">
  INSERT INTO auction_action(paidid,action_user,action_note,log_time)
  VALUE(#rs.GENERATEDKEY#,'#Mobile#','会员手机创建',#now()#)
  </cfquery>  
  <cfreturn rs.GENERATEDKEY>
<cfelse>
  <cfreturn '操作超时'>
</cfif>

</cffunction>









<!---订单显示--->
<cffunction access="remote" name="showwonpaid" returntype="query">
<cfquery datasource="#DB#" name="showwonpaid">
SELECT
auction_userwonpaid.paidID,
auction_userwonpaid.acttime,
auction_userwonpaid.ship_name,
auction_userwonpaid.ship_area,
auction_userwonpaid.ship_addr,
auction_userwonpaid.final_amount,
auction_userwonpaid.payed
FROM
auction_userwonpaid
WHERE paidID = #paidID# AND uid = #session.user.uid# AND auction_userwonpaid.acttime IS NOT NULL
</cfquery>
<cfreturn showwonpaid>
</cffunction>

<!---已支付单--->
<cffunction access="remote" name="paidlist" returntype="query">
<cfquery datasource="#DB#" name="paidlist">

select  d.paidID,e.billno,e.acttime,e.paytime,e.final_amount,e.payment,e.remark  from auction_userlastbid  a  left outer join auction b on a.aid=b.aid   left outer join auction_special c on b.asid=c.asid  left outer join auction_userwon d on a.wonID=d.wonID left outer join auction_userwonpaid e on d.paidID=e.paidID   where a.uid=#session.user.uid# 
and a.is_bid=1 and a.pay_status=2  and a.aid in (select aid from auction where asid in (select asid from auction_special where now() > asclosetime) )


</cfquery>
<cfreturn paidlist>
</cffunction>


</cfcomponent>